package com.ateam.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ateam.biz.ActivitiesBiz;
import com.ateam.biz.SharesBiz;
import com.ateam.pojo.Activities;
import com.ateam.pojo.Shares;
import com.ateam.pojo.Users;
import com.ateam.util.DateFactory;
import com.ateam.util.PojoFactory;
import com.ateam.util.SplitFactory;

/**
 * @author WuYuan
 * 活动：主要是提交活动
 */
@Controller
public class ActivitiesController {
	@Resource(name = "activitiesBiz")
	private ActivitiesBiz activitiesBiz;

	@Resource(name = "sharesBiz")
	private SharesBiz sharesBiz;

	// 公司范围添加活动
	@ResponseBody
	@RequestMapping("addactivity.do")
	public boolean addactivity(String activityname, String activitymemberid, String activitydescription,
			String starttime, String endtime, String activityaddress, HttpSession session, HttpServletRequest request) {

		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				int[] memberids = SplitFactory.getIntarray(activitymemberid);
				Activities activity = PojoFactory.getActivity();
				activity.setActivityname(activityname);
				activity.setDescription(activitydescription);
				activity.setStarttime(DateFactory.getStrToDate(starttime));
				activity.setEndtime(DateFactory.getStrToDate(endtime));
				activity.setActivityaddress(activityaddress);

				if (0 != activitiesBiz.addActivity(activity, memberids, 1, 1)) {

					Users user = (Users) session.getAttribute("currentUser");
					Shares share = PojoFactory.getShare();
					share.setContent("发布了活动:" + activityname + "<h5>" + starttime + "</h5> 到 <h5>" + endtime
							+ "</h5>之间  于  " + activityaddress + "  进行活动:<br />" + activitydescription);
					sharesBiz.addShareByUser(share, user, 1, 1);
					// 如果任务发布成功，那么增加一条share描述这个过程
					return true;
				}
				return false;
			}
			return false;
		}
		return false;
	}

	// 项目范围添加活动
	@ResponseBody
	@RequestMapping("projectaddactivity.do")
	public boolean projectaddactivity(String activityname, String projectid, String activitymemberid,
			String activitydescription, String starttime, String endtime, String activityaddress, HttpSession session,
			HttpServletRequest request) {

		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				int[] memberids = SplitFactory.getIntarray(activitymemberid);
				Activities activity = PojoFactory.getActivity();
				activity.setActivityname(activityname);
				activity.setDescription(activitydescription);
				activity.setStarttime(DateFactory.getStrToDate(starttime));
				activity.setEndtime(DateFactory.getStrToDate(endtime));
				activity.setActivityaddress(activityaddress);

				if (0 != activitiesBiz.addActivity(activity, memberids, 1, Integer.parseInt(projectid))) {

					Users user = (Users) session.getAttribute("currentUser");
					Shares share = PojoFactory.getShare();
					share.setContent("发布了活动:" + activityname + "<h5>" + starttime + "</h5> 到 <h5>" + endtime
							+ "</h5>之间  于  " + activityaddress + "  进行活动:<br />" + activitydescription);
					sharesBiz.addShareByUser(share, user, 1, Integer.parseInt(projectid));
					// 如果任务发布成功，那么增加一条share描述这个过程
					return true;
				}
				return false;
			}
			return false;
		}
		return false;
	}

	//小组层面提交活动
	@ResponseBody
	@RequestMapping("groupaddactivity.do")
	public boolean groupaddactivity(String activityname, String groupid, String activitymemberid,
			String activitydescription, String starttime, String endtime, String activityaddress, HttpSession session,
			HttpServletRequest request) {

		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				int[] memberids = SplitFactory.getIntarray(activitymemberid);
				Activities activity = PojoFactory.getActivity();
				activity.setActivityname(activityname);
				activity.setDescription(activitydescription);
				activity.setStarttime(DateFactory.getStrToDate(starttime));
				activity.setEndtime(DateFactory.getStrToDate(endtime));
				activity.setActivityaddress(activityaddress);

				if (0 != activitiesBiz.addActivity(activity, memberids, Integer.parseInt(groupid), 1)) {

					Users user = (Users) session.getAttribute("currentUser");
					Shares share = PojoFactory.getShare();
					share.setContent("发布了活动:" + activityname + "<h5>" + starttime + "</h5> 到 <h5>" + endtime
							+ "</h5>之间  于  " + activityaddress + "  进行活动:<br />" + activitydescription);
					sharesBiz.addShareByUser(share, user, Integer.parseInt(groupid), 1);
					// 如果任务发布成功，那么增加一条share描述这个过程
					return true;
				}
				return false;
			}
			return false;
		}
		return false;
	}
}
